package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.WorkspaceMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 工作台业务实现类
 * @author admin
 * @date 2024/10/8
 */
@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private WorkspaceMapper workspaceMapper;
    @Autowired
    private ReportMapper reportMapper;

    /**
     * 获取营业数据
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime beginTime, LocalDateTime endTime) {


        Map<String, Object> map = new HashMap<>();
        map.put("begin", beginTime);
        map.put("end", endTime);
        // 查询新增的用户数量
        Integer newUsers = reportMapper.getUserStatistics(map);
        newUsers = newUsers == null ? 0 : newUsers;

        // 获取订单完成率
        Integer totalOrderCount = reportMapper.getOrderCount(map);
        map.put("status", Orders.COMPLETED);
        Integer validOrderCount = reportMapper.getOrderCount(map);
        Double orderCompletionRate = 0.0;
        if (totalOrderCount != 0) {
            orderCompletionRate = validOrderCount / totalOrderCount.doubleValue();
        }
        // 获取营业额
        Double turnover = reportMapper.getTurnoverStatistics(map);
        turnover = turnover == null ? 0.0 : turnover;
        // 平均客单价
        double unitPrice = 0.0;
        if(validOrderCount != 0) {
             unitPrice = Math.floor(turnover / validOrderCount);
        }
        unitPrice = unitPrice == 0.0 ? 0.0 : unitPrice;
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .build();
    }


    /**
     * 获取套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
    //     获取停售，起售套餐数量
        Integer status = StatusConstant.DISABLE;
    //     获取discontinued
        Integer discontinued = workspaceMapper.getOverviewSetmeals(status);
    //     获取sold
        status = StatusConstant.ENABLE;
        Integer sold = workspaceMapper.getOverviewSetmeals(status);

        return new SetmealOverViewVO(sold, discontinued);
    }

    /**
     * 获取菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getOverviewDishes() {
    //     获取停售，起售菜品数量
        Integer status = StatusConstant.DISABLE;
        Integer discontinued = workspaceMapper.getOverviewDishes(status);
        status = StatusConstant.ENABLE;
        Integer sold = workspaceMapper.getOverviewDishes(status);
        return new DishOverViewVO(sold, discontinued);
    }


    /**
     * 获取订单总览
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrders() {
    //     获取全部订单数量，待派送订单，待接单订单，已完成订单，已取消订单
        LocalDate now = LocalDate.now();
        LocalDateTime begin = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(now, LocalTime.MAX);

        Map<String, Object> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        // 全部订单数量
        Integer allOrders = reportMapper.getOrderCount(map);
    //     已取消
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = reportMapper.getOrderCount(map);
    //     已完成
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = reportMapper.getOrderCount(map);
    //     待派送
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = reportMapper.getOrderCount(map);
    //     待接单
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = reportMapper.getOrderCount(map);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }
}
